<?php
class Article extends MY_Controller {
	const IMAGE_TYPES = 'gif|jpg|png';
	const MAX_SIZE_IMAGE = '2000';
	protected $_controller_path = '/article/Article';
	protected $_list_path = '/article/list';
	protected $_model = 'article/Article_model';
	protected $_parent_model = 'catalog/Catalog_model';
	protected $_parent_id = 'catalog_id';
	protected $_is_search_parent = TRUE;
	protected $_create_path = 'article/create';
	protected $_view_path = 'article/view';
	protected $_edit_path = 'article/edit';
	protected $_sort_id_type = 'DESC';
	protected $_resource_id = self::ARTICLE_RESOURCE;

	function create() {
		$this -> setFormValidation();
		if ($this -> form_validation -> run() == false) {
			$data = array();
			if(isset($this->_parent_model)) {
				$this -> load -> model($this->_parent_model, 'parent_model');
				$data['parent_list'] = $this -> parent_model -> get_many_by(array('is_active' => 1));
			}
			$this->load->view($this -> _create_path, $data);
		} else {
			//upload image
			$config = array('upload_path' => realpath(APPPATH . "../images"), 
							'allowed_types' => self::IMAGE_TYPES, 
							'max_size' => self::MAX_SIZE_IMAGE,
						);
		$this -> load -> library("upload", $config);
			if (!$this -> upload -> do_upload('image_path')) {
				$error = array('error' => $this -> upload -> display_errors());
				print_r($error);
			} else {
				$upload_data = $this -> upload -> data();
				//save information of image
				$this -> load -> model('image/Image_Model', 'image_model');
				$image_data = array(
					'title' => $upload_data['file_name'],
					'url'   => base_url().'images/'.$upload_data['file_name'],
					'alt'   => '' //TODO: set later
					
				);
				$image_id = $this -> image_model -> insert($image_data);
				$data['result'] = $this->init_input_data();
				add_to_array($data['result'], array('image_id' => $image_id));
				$id = $this -> model -> insert($data['result']);
				$this->getList();
			}
		}
	}

	function show($id) {
		$this -> load -> model($this->_parent_model, 'catalog_model');
		$this -> load -> model('image/Image_Model', 'image_model');
		$data['result'] = $this -> model -> get($id);
		$data['catalog'] = $this -> catalog_model -> get($data['result'] -> catalog_id);
		$data['image'] = $this -> image_model -> get($data['result'] -> image_id);
		$this->load->view($this -> _view_path, $data);
	}
	
	function update() {
		$this -> setFormValidation();

		if ($this -> input -> post('action') == "save") {
			$id = $this -> input -> post('id');
			if ($this -> form_validation -> run() == true) {
				$config = array('upload_path' => realpath(APPPATH . "../images"), 
							'allowed_types' => self::IMAGE_TYPES, 
							'max_size' => self::MAX_SIZE_IMAGE,
						);
			$this -> load -> library("upload", $config);
			if (!$this -> upload -> do_upload('image_path')) {
				$error = array('error' => $this -> upload -> display_errors());
				// print_r($error);
			} else {
				$upload_data = $this -> upload -> data();
				//save information of image
				$this -> load -> model('image/Image_Model', 'image_model');
				$image_data = array(
					'title' => $upload_data['file_name'],
					'url'   => base_url().'images/'.$upload_data['file_name'],
					'alt'   => '' //TODO: set later
					
				);
				$data['result'] = $this->init_input_data();
				$image_id = $this -> input -> post('image_id');
				$this -> image_model -> update($image_id, $image_data);
				$this -> model -> update($id, $data['result']);
			}
				$data['inputs'] = $this->init_input_data();
				$this -> model -> update($id, $data['inputs']);
				$this -> show($id);
			} else {//validation fail
				//TODO: need to fix
				$data = $this -> model -> get($id);
				$this->load->view($this -> _edit_path, $data);
			}
		} else {
			$id = $this -> uri -> segment(self::URI_SEGMENT);
			$data['params'] = $this -> model -> get($id);

			$this -> load -> model('catalog/Catalog_Model', 'catalog_model');
			$this -> load -> model('image/Image_Model', 'image_model');
			$data['image'] = $this -> image_model -> get($data['params'] -> image_id);
			$data['catalog_list'] = $this -> catalog_model -> get_many_by(array('is_active' => 1));
			$this->load->view($this -> _edit_path, $data);
		}
	}

	protected function setFormValidation() {
		$this -> form_validation -> set_rules('title', 'Title name', 'trim|required');
	}
	
	protected function setQuickEditFormValidation($id) {
		$this -> form_validation -> set_rules('title'.$id, 'Title name', 'trim|required');
	}
	
	protected function init_input_data() {
		$title = $this -> input -> post('title');
		$image_path = $this -> input -> post('image_path');
		$summary = $this -> input -> post('summary');
		$catalog_id = $this -> input -> post('catalog_id');
		$image_id = $this -> input -> post('image_id');
		$arr_date =explode('/', $this -> input -> post('created_date'));
		$created_date = $arr_date[2].'-'.$arr_date[0].'-'.$arr_date[1];
		
		$meta_data = unsign($title.self::META_SEPERATE.$created_date.self::META_SEPERATE.$summary);
		
		return array('title' => $title, 
								'created_date' => $created_date, 
								'summary' => $summary, 
								'catalog_id' => $catalog_id,
								'image_id' => $image_id,
								'meta_data' => $meta_data,
								);
	}

	protected function init_quick_input_data($id) {
		$title = $this -> input -> post('title'.$id);
		$summary = $this -> input -> post('summary'.$id);
		
		$arr_date =explode('/', $this -> input -> post('created_date'.$id));
		$created_date = $arr_date[2].'-'.$arr_date[0].'-'.$arr_date[1];
		
		$meta_data = unsign($title.self::META_SEPERATE.$created_date.self::META_SEPERATE.$summary);
		return array(
				'title' => $title, 
				'created_date' => $created_date,
				'summary' => $summary, 
				'meta_data' => $meta_data
		);
	}
}
?>